<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组合式继承</title>
</head>
<body>

<script type="text/javascript">

  // 声明父类
  const SuperClass = function (name) {
    this.name = name
    this.books = ['javascript', 'typescript', 'react', 'vue3']
  }

  // 声明父类原型上的方法
  SuperClass.prototype.showBooks = function () {
    console.log(this.books)
  }

  // 声明子类
  const SubClass = function (name) {
    this.name = name
  }

  // 子类继承父类（链式继承）
  // 通过继承父类的原型对象避免内存浪费（主要针对方法），属性在父类中通过this定义
  SubClass.prototype = new SuperClass()

  const subClass1 = new SubClass('java')
  const subClass2 = new SubClass('php')
  subClass2.showBooks()
  subClass1.books.push('ios')
  console.log(subClass1.books)
  console.log(subClass2.books)

</script>

</body>
</html>
